package day05;

/**
 * @author yzg
 * @desc TODO
 * @date 2025/4/14
 */
public class SequenceSum {
    public static void main(String[] args) {
        double sum = sequenceSum(20);
        System.out.println("数列前20项和为：" + sum );
    }

    public static double sequenceSum(int n) {
        if (n <= 0) {
            return 0;
        }

        double sum = 0;
        double up = 2; // 分子
        double dowm = 1; // 分母

        for (int i = 1; i <= n; i++) {
            sum += up / dowm;

            // 计算下一项的分子和分母
            double nextUp = up + dowm;
            double nextDown = up;

            up = nextUp;
            dowm = nextDown;
        }

        return sum;
    }
}
